# -*- coding: utf-8 -*-
import os
from os import chdir as cd

HERE = os.path.abspath(os.path.dirname(__file__))


def convert(app=''):
    ignore = ['accounts', 'collect', 'resources', 'frontend', 'core', 'top', 'restauth', 'upload', 'store', 'tags']
    newdir = os.path.join(HERE, 'restful')

    if app != '':
        with lcd(os.path.join(HERE, 'contrib', app)):
            print app
    else:
        for app in os.listdir('contrib'):
            app_path = os.path.join('contrib', app)

            if os.path.isdir(app_path):
                if app not in ignore:
                    with cd(os.path.join(HERE, 'contrib', app)):
                        if os.path.exists('models.py'):
                            models = open('models.py').read()
                            print "open(os.path.join(%s, 'models', '%s.py' % app), 'w').write(%s)" % (
                                newdir, app, models)

                        if os.path.exists('admin.py'):
                            models = open('admin.py').read()
                            print "open(os.path.join(%s, 'models', '%s.py' % app), 'w').write(%s)" % (
                                newdir, app, models)

                        if os.path.exists('serializers.py'):
                            models = open('serializers.py').read()
                            print "open(os.path.join(%s, 'models', '%s.py' % app), 'w').write(%s)" % (
                                newdir, app, models)


if __name__ == '__main__':
    convert()
